<template>
    <div>
        <h2>创建账号</h2>
        <button @click="createAccount">创建账号</button>
        <div v-if="result">
            <p><strong>账号:</strong> {{ result.pub_key_hash }}</p>
            <!-- <p><strong>密码:</strong> {{ result.password }}</p> -->
        </div>
    </div>
</template>

<script>
import axios from 'axios'
import { ref } from 'vue'

export default {
    name: 'CreateAccount',
    setup() {
        const result = ref(null)

        const createAccount = async () => {
            try {
                // 向服务器发起创建账号请求
                const response = await axios.post('/api/account/create')
                // 假设返回的数据格式为：{ pub_key_hash: Array(20) }
                // 将 pub_key_hash 转换为 16 进制字符串
                const pubKeyHashHex = Array.from(new Uint8Array(response.data.pub_key_hash))
                    .map(byte => byte.toString(16).padStart(2, '0'))
                    .join('')
                result.value = { pub_key_hash: pubKeyHashHex }
                console.log(result.value)
            } catch (error) {
                console.error(error)
                alert('创建账号失败')
            }
        }

        return { createAccount, result }
    }
}
</script>